package org.gwt.mosaic.desktop.client;

import java.util.HashMap;
import java.util.Map;

import org.gwt.mosaic.application.client.Application;
import org.gwt.mosaic.ui.client.DefaultDesktopManager;
import org.gwt.mosaic.ui.client.ToolBar;
import org.gwt.mosaic.ui.client.ToolButton;
import org.gwt.mosaic.ui.client.WindowPanel;
import org.gwt.mosaic.ui.client.ToolButton.ToolButtonStyle;
import org.gwt.mosaic.ui.client.WindowPanel.WindowState;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.user.client.ui.PopupPanel;

public class GwtDEDesktopManager extends DefaultDesktopManager {

  private Map<WindowPanel, ToolButton> map = new HashMap<WindowPanel, ToolButton>();

  @Override
  public void onOpen(OpenEvent<WindowPanel> event) {
    super.onOpen(event);

    final WindowPanel windowPanel = event.getTarget();

    if (windowPanel instanceof ControlPanel) {
      return;
    }

    ToolButton toolButton = new ToolButton(windowPanel.getCaption());
    toolButton.setStyle(ToolButtonStyle.RADIO);
    toolButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        if (windowPanel.getWindowState() == WindowState.MINIMIZED) {
          windowPanel.setWindowState(WindowState.NORMAL);
          windowPanel.toFront();
        } else if (windowPanel.isActive()) {
          windowPanel.setWindowState(WindowState.MINIMIZED);
        } else {
          windowPanel.toFront();
        }
      }
    });

    map.put(windowPanel, toolButton);

    GwtDE app = (GwtDE) Application.getInstance();
    ToolBar toolBar = app.getToolBar();
    toolBar.add(toolButton);
    toolBar.layout();
  }

  @Override
  public void onClose(CloseEvent<PopupPanel> event) {
    super.onClose(event);

    WindowPanel windowPanel = (WindowPanel) event.getTarget();

    if (windowPanel instanceof ControlPanel) {
      return;
    }

    ToolButton toolButton = map.get(windowPanel);

    map.remove(windowPanel);

    toolButton.removeFromParent();

    layoutTollBar();
  }

  @Override
  public void onSelection(SelectionEvent<WindowPanel> event) {
    super.onSelection(event);

    WindowPanel windowPanel = (WindowPanel) event.getSelectedItem();

    if (windowPanel instanceof ControlPanel) {
      return;
    }

    ToolButton toolButton = map.get(windowPanel);
    toolButton.setChecked(true);

    layoutTollBar();
  }

  @Override
  public void minimize(WindowPanel w, WindowState oldWindowState) {
    super.minimize(w, oldWindowState);

    if (w instanceof ControlPanel) {
      return;
    }

    ToolButton toolButton = map.get(w);
    toolButton.setChecked(false);

    layoutTollBar();
  }

  private static void layoutTollBar() {
    GwtDE app = (GwtDE) Application.getInstance();
    ToolBar toolBar = app.getToolBar();
    toolBar.layout();
  }

}
